# Responsability
# Decide de best posterior

PosteriorDecider <- setRefClass(    
  "posteriordecider"
  , fields = list(
    accepted_countoutput="data.frame",
    measurements="data.frame"
  )
  , methods = list(
    #
    #
    # Constructor
    #
    #
    initialize = function(..., 
                          output = "",
                          measurements = "")
    {
      #TODO: validate data 
      print("Initialize Likelihood")
      callSuper(...,
                output = output,
                measurements = measurements)
    },
    dummyRandom = function() {
      
      rnd <- runif(1, 1.0, 17.5)
      
      loginfo("Dummy Random")
      loginfo(rnd)
      
      rnd
    },
    #
    #
    #
    #
    #
    accept = function(newPosterior, lastPosterior) {
          
      logAlpha  <- (newPosterior) - (lastPosterior)
      logRand   <- log(runif(1)) #Always under 0
          
      if (logAlpha >= logRand)
      {
        chain[[j]]@accepted_count <- chain[[j]]@accepted_count + 1.0 
        chain[[j]]@parameter <- chain[[j]]@parameterCandidate
        
        chain[[j]]@last_posterior <- newPosterior
        
        if ( newPosterior > chain[[j]]@best_posterior )
        {
          chain[[j]]@best_posterior <-  newPosterior
          
          chain[[j]]@best_count <- chain[[j]]@best_count + 1
          print("New best Loglikelyhood:")
          print(chain[[j]]@best_posterior)
        }
      }
      
      acceptanceRate <- chain[[j]]@accepted_count / i
    },
    calculatePosterior = function(logLikelihood) {
      sum(logLikelihood)
    }



